package dsk.demo.product.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import dsk.demo.product.entity.AutoSendMailConfigEntity;
import dsk.demo.product.entity.CantonBuyerEntity;
import dsk.demo.product.entity.CustomerLinkManEntity;
import dsk.demo.product.entity.MailtempEntity;
import dsk.demo.product.service.AutoSendMailConfigService;
import dsk.demo.product.service.CantonBuyerService;
import dsk.demo.product.service.CustomerLinkManService;
import dsk.demo.product.service.MailtempService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

/**
 * 自动邮件发送定时任务
 */
@Component
public class AutoSendMailConfig {

    @Autowired
    private MailtempService mailtempService;

    @Autowired
    private CustomerLinkManService customerLinkManService;

    @Autowired
    private AutoSendMailConfigService autoSendMailConfigService;

    @Autowired
    private CantonBuyerService cantonBuyerService;

    /**
     * 每日6点 定时发送邮件
     */
    @Scheduled(cron = "0 0 6 * * ?")
    public void autoSendMail() throws IOException {
        //1. 从数据库中获取配置实体类,每个实体使用一个线程来执行邮件发送工作.  暂定.
        QueryWrapper<AutoSendMailConfigEntity> query = new QueryWrapper<>();
        query.ne("status", "2");
        List<AutoSendMailConfigEntity> list = autoSendMailConfigService.list(query);
        for (AutoSendMailConfigEntity entity : list) {
            if (entity.getType() == 1) {
                sendMailByHsCode(entity);
            } else if (entity.getType() == 2) {
                sendMailByIndustry(entity);
            }
        }
    }

    private void sendMailByHsCode(AutoSendMailConfigEntity entity) {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(entity.getStmp());
        javaMailSender.setUsername(entity.getUsername());
        javaMailSender.setPassword(entity.getPassword());
        javaMailSender.setDefaultEncoding("utf-8");
        Properties javaMailProperties = new Properties();
        javaMailProperties.setProperty("mail.smtp.auth", "true");
        javaMailProperties.setProperty("mail.smtp.starttls.enable", "true");
        javaMailProperties.setProperty("mail.smtp.starttls.required", "true");
        javaMailSender.setJavaMailProperties(javaMailProperties);
        MailtempEntity byId = mailtempService.getById(entity.getMailtempId());
        if (byId == null) {
            entity.setStatus("2");
            autoSendMailConfigService.updateById(entity);
            return;
        }
        //获取当天的批次
        String companyId = entity.getCompanyId();
        List<CustomerLinkManEntity> autoSendLinkMan = customerLinkManService.getAutoSendLinkMan(companyId, entity.getHscode());
        for (CustomerLinkManEntity customerLinkManEntity : autoSendLinkMan) {
            try {
                //创建SimpleMailMessage对象
//                SimpleMailMessage message = new SimpleMailMessage();
                MimeMessage mimeMessage = javaMailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
                //邮件发送人
                helper.setFrom(entity.getUsername());
                //邮件接收人
                helper.setTo(customerLinkManEntity.getEmail());
                //邮件主题
                helper.setSubject(byId.getTitle());
                //邮件内容
                helper.setText(byId.getContent(), true);
                //发送邮件
                javaMailSender.send(mimeMessage);
            } catch (Exception e) {
                System.out.println("发送邮件时发生异常！" + e);
            }
        }
        //更新这个批次的状态
        if (autoSendLinkMan.size() > 0) {
            CustomerLinkManEntity customerLinkManEntity = autoSendLinkMan.get(autoSendLinkMan.size() - 1);
            entity.setCompanyId(customerLinkManEntity.getCompanyId());
            entity.setStatus("1");
            autoSendMailConfigService.updateById(entity);
        } else {
            entity.setStatus("2");
            autoSendMailConfigService.updateById(entity);
        }
    }

    //广交会邮件自动发送
    private void sendMailByIndustry(AutoSendMailConfigEntity entity) {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(entity.getStmp());
        javaMailSender.setUsername(entity.getUsername());
        javaMailSender.setPassword(entity.getPassword());
        javaMailSender.setDefaultEncoding("utf-8");
        Properties javaMailProperties = new Properties();
        javaMailProperties.setProperty("mail.smtp.auth", "true");
        javaMailProperties.setProperty("mail.smtp.starttls.enable", "true");
        javaMailProperties.setProperty("mail.smtp.starttls.required", "true");
        javaMailSender.setJavaMailProperties(javaMailProperties);
        MailtempEntity byId = mailtempService.getById(entity.getMailtempId());
        if (byId == null) {
            entity.setStatus("2");
            autoSendMailConfigService.updateById(entity);
            return;
        }
        //获取当天的批次
        String companyId = entity.getCompanyId();
        List<CantonBuyerEntity> autoSendBuyers = cantonBuyerService.getAutoSendBuyer(companyId, entity.getIndustry());
        for (CantonBuyerEntity buyer : autoSendBuyers) {
            try {
                MimeMessage mimeMessage = javaMailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
                //邮件发送人
                helper.setFrom(entity.getUsername());
                //邮件接收人
                helper.setTo(buyer.getEmail());
                //邮件主题
                helper.setSubject(byId.getTitle());
                //邮件内容
                helper.setText(byId.getContent(), true);
                //发送邮件
                javaMailSender.send(mimeMessage);
            } catch (Exception e) {
                System.out.println("发送邮件时发生异常！" + e);
            }
        }
        //更新这个批次的状态
        if (autoSendBuyers.size() > 0) {
            CantonBuyerEntity buyer = autoSendBuyers.get(autoSendBuyers.size() - 1);
            entity.setCompanyId(buyer.getId() + "");
            entity.setStatus("1");
            autoSendMailConfigService.updateById(entity);
        } else {
            entity.setStatus("2");
            autoSendMailConfigService.updateById(entity);
        }
    }
}
